/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telsoft.cms_admin.utility;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import telsoft.cms_admin.data.DATA_FormServices;
import telsoft.cms_admin.entity.ETT_FormServices;
import telsoft.cms_admin.entity.ETT_FormSystem;
import telsoft.cms_admin.util.BaseController;
import telsoft.util.StringUtil;

/**
 *
 * @author hoannc
 */
@ManagedBean(name = "services")
@ViewScoped
public class FUNC_FormServices extends BaseController implements Serializable {

    private boolean checkClearField = true;
    private ETT_FormServices selectedService;
    private ETT_FormServices ETT_FormServices;
    private ETT_FormServices ETT_FormServices_old;
    private List<ETT_FormServices> listData = new ArrayList<ETT_FormServices>();
    private List<ETT_FormSystem> listUser2Group = new ArrayList<ETT_FormSystem>();
    private boolean blSelected = false;
    private ETT_FormSystem ETT_System;
    private ETT_FormSystem selectedSys;
    public DATA_FormServices model = new DATA_FormServices();

    public FUNC_FormServices() {
//        super();
        try {
            init();
        } catch (Exception ex) {
            addErrorMessage(ex);
            ex.printStackTrace();
        }

    }

    public boolean isCheckClearField() {
        return checkClearField;
    }

    public void setCheckClearField(boolean checkClearField) {
        this.checkClearField = checkClearField;
    }
    
    private void init() throws Exception {
        try {
//            listUser2Group = DataUtil.getData(DATA_FormServices.class, "getUser2Group");
            listUser2Group = model.getUser2Group();
            if (listUser2Group.isEmpty()) {
                ETT_FormServices = new ETT_FormServices();
                selectedSys = new ETT_FormSystem();
                bDisableModifyRemove = false;
                bDisableAddSearch = true;
                blSelected = false;
            } else {
                ETT_System = listUser2Group.get(0);
                selectedSys = ETT_System;
                listData = model.loadDetailProduct(ETT_System);
//                listData = (List<ETT_FormServices>) DataUtil.performAction(DATA_FormServices.class, "loadDetailProduct", ETT_System);
                if (listData.isEmpty()) {
                    ETT_FormServices = new ETT_FormServices();
                    bDisableModifyRemove = false;
                    bDisableAddSearch = false;
                    blSelected = false;
                } else {
                    ETT_FormServices = listData.get(0);
                    selectedService = ETT_FormServices;
                    bDisableModifyRemove = true;
                    bDisableAddSearch = false;
                    blSelected = true;
                }
            }
            bFieldEnable = true;
        } catch (Exception ex) {
            addErrorMessage(ex);
            ex.printStackTrace();
        }
    }

    public void onSelectRow() {
        try {
//            ETT_System = event;
//            listData = (List<ETT_FormServices>) DataUtil.performAction(DATA_FormServices.class, "loadDetailProduct", selectedSys);
            listData = model.loadDetailProduct(selectedSys);
            if (listData.isEmpty()) {
                ETT_FormServices = new ETT_FormServices();
                bDisableModifyRemove = false;
//                bFieldEnable = true;
//                bDisableAddSearch = false;
                blSelected = false;
            } else {
                ETT_FormServices = listData.get(0);
                selectedService = ETT_FormServices;
                bDisableModifyRemove = true;
//                bFieldEnable = false;
//                bDisableAddSearch = false;
                blSelected = true;
            }
        } catch (Exception ex) {
            addErrorMessage(ex);
            ex.printStackTrace();
        }
    }

    public void changeState(int iAction, boolean bNormalState) {
        if (bNormalState) {
            bRendered = false;
            bCancel = true;
            bSave = true;
        } else {
            bRendered = true;
            bCancel = false;
            bSave = false;
        }
        onChangeAction(iAction);
        setFieldEnable(bNormalState);
    }

    private void add() {
        try {
            if (validateInput()) {
                ETT_FormServices ETT_ServicesData = new ETT_FormServices();
                ETT_FormServices.setSYS_ID(selectedSys.getSYS_ID());
//                ETT_ServicesData = (ETT_FormServices) DataUtil.performAction(DATA_FormServices.class, "insert", ETT_FormServices);
                ETT_ServicesData = model.insert(ETT_FormServices);
                listData.add(ETT_ServicesData);
                //set selected row
                selectedService = ETT_ServicesData;
                bDisableModifyRemove = true;
                blSelected = false;
            } else {
                fillDetailValue();
            }
        } catch (Exception ex) {
            addErrorMessage(ex);
            fillDetailValue();
            ex.printStackTrace();
        }
    }

    private boolean remove() {
        try {
            model.delete(selectedService);
            //Update user interface
            listData.remove(selectedService);

            //set selected row
            if (listData.isEmpty()) {
                bDisableModifyRemove = false;
            } else {
                selectedService = listData.get(0);
                ETT_FormServices = selectedService;
            }
            return true;
        } catch (Exception ex) {
            addErrorMessage("Không thể xóa, tồn tại dữ liệu liên quan!");
            fillDetailValue();
            ex.printStackTrace();
            return false;
        }
    }

//    private void search() {
//        try {
//            listData = (List<ETT_FormServices>) DataUtil.performAction(DATA_FormServices.class, "search", ETT_FormServices, ETT_FormServices);
//            //Update UI
//            if (listData.isEmpty()) {
//                bDisableModifyRemove = false;
//                bFieldEnable = true;
//                bDisableAddSearch = true;
//                blSelected = false;
//                listData = new ArrayList<ETT_FormServices>();
//                selectedService = new ETT_FormServices();
//            } else {
//                bDisableModifyRemove = true;
//                bFieldEnable = true;
//                bDisableAddSearch = false;
//                blSelected = true;
//                selectedService = listData.get(0);
//                ETT_FormServices = selectedService;
//            }
//        } catch (Exception ex) {
//            addErrorMessage(ex);
//            fillDetailValue();
//            ex.printStackTrace();
//        }
//    }
    private void modify() {
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        try {
            if (validateInput()) {
//                DataUtil.performAction(DATA_FormServices.class, "update", ETT_FormServices);
                model.update(ETT_FormServices);
                ETT_FormServices.setStrCREATED_TIME(df.format(selectedService.getCREATED_TIME()));
                ETT_FormServices.setStrEFFECTIVE_TIME(df.format(selectedService.getEFFECTIVE_TIME()));
                ETT_FormServices.setStrEXPIRY_TIME(df.format(selectedService.getEXPIRY_TIME()));
                //Update user interface
                int i = 0;
                for (ETT_FormServices item : listData) {
                    if (item.getSER_ID().equals(ETT_FormServices.getSER_ID())) {
                        listData.set(i, ETT_FormServices);
                        selectedService = ETT_FormServices;
                        break;
                    }
                    i++;
                }
            } else {
                fillDetailValue();
            }
        } catch (Exception ex) {
            addErrorMessage(ex);
            fillDetailValue();
            ex.printStackTrace();
        }
    }

    public void onRowSelect() {
        try {
            bDisableModifyRemove = true;
            ETT_FormServices = (ETT_FormServices) selectedService.clone();
        } catch (Exception ex) {
            addErrorMessage(ex);
            ex.printStackTrace();
        }
    }

    public void onChangeAction(int iOld_Action) {
        if (iOld_Action == ACTION_ADD || iOld_Action == ACTION_SEARCH
                || iOld_Action == ACTION_REMOVE || iOld_Action == ACTION_MODIFY || iOld_Action == ACTION_ADDCOPY) {
            if (iOld_Action == ACTION_ADD) {
                iAction = ACTION_ADD;
                if(checkClearField) clearFieldValue();
            } else if (iOld_Action == ACTION_MODIFY) {
                iAction = ACTION_MODIFY;
                clearFieldValue();
                fillDetailValue();
            } else if (iOld_Action == ACTION_ADDCOPY) {
                iAction = ACTION_ADDCOPY;
                clearFieldValue();
                fillDetailValue();
            } else if (iOld_Action == ACTION_SEARCH) {
                iAction = ACTION_SEARCH;
                clearFieldValue();
            } else if (iOld_Action == ACTION_REMOVE) {
                iAction = ACTION_REMOVE;
                clearFieldValue();
                doAction();
            }
        } else if (iOld_Action == ACTION_SAVE || iOld_Action == ACTION_CANCEL) {
            if (iOld_Action == ACTION_SAVE) {
                doAction();
            } else if (iOld_Action == ACTION_CANCEL) {
                fillDetailValue();
            }
        }
    }

    public void fillDetailValue() {
        try {
            ETT_FormServices = (ETT_FormServices) ETT_FormServices_old.clone();
        } catch (Exception ex) {
            addErrorMessage(ex);
            ex.printStackTrace();
        }
    }

    public void clearFieldValue() {
        try {
            ETT_FormServices_old = (ETT_FormServices) ETT_FormServices.clone();
            ETT_FormServices = new ETT_FormServices();
        } catch (Exception ex) {
            addErrorMessage(ex);
            ex.printStackTrace();
        }
    }

    public void doAction() {
        switch (iAction) {
            case ACTION_ADD:
                add();
                break;
            case ACTION_ADDCOPY:
                add();
                break;
            case ACTION_MODIFY:
                modify();
                break;
            case ACTION_SEARCH:
//                search();
                break;
            case ACTION_REMOVE:
                remove();
                break;
        }
    }

    public boolean validateInput() {
        if (StringUtil.nvl(ETT_FormServices.getSER_NAME(), "").equals("")) {
            addErrorMessage("Trường 'Tên nội dung' không được để trống!");
            checkClearField = false;
            return false;
        }
        if (StringUtil.nvl(ETT_FormServices.getSER_CODE(), "").equals("")) {
            addErrorMessage("Trường 'Tên nội dung' không được để trống!");
            checkClearField = false;
            return false;
        }
        if (ETT_FormServices.getCREATED_TIME() == null) {
            addErrorMessage("Trường 'Ngày khởi tạo' không được để trống!");
            checkClearField = false;
            return false;
        }
        if (ETT_FormServices.getEFFECTIVE_TIME() == null) {
            addErrorMessage("Trường 'Thời gian có hiệu lực' không được để trống!");
            checkClearField = false;
            return false;
        }
        if (ETT_FormServices.getEXPIRY_TIME() == null) {
            addErrorMessage("Trường 'Thời gian hết hiệu lực' không được để trống!");
            checkClearField = false;
            return false;
        }
        if (StringUtil.nvl(ETT_FormServices.getSTATUS(), "").equals("")) {
            addErrorMessage("Trường 'Trạng thái' phải được chọn!");
            checkClearField = false;
            return false;
        }
        if (StringUtil.nvl(ETT_FormServices.getSER_NUMBER(), "").equals("")) {
            addErrorMessage("Trường 'Đầu số dịch vụ' không được để trống và phải nhập là số theo định dạng xxxx!");
            checkClearField = false;
            return false;
        }
        checkClearField = true;
        return true;
    }

    public void setFieldEnable(boolean b) {
        if (b) {
            bFieldEnable = false;
        } else {
            bFieldEnable = true;
        }
    }

    public ETT_FormServices getSelectedRow() {
        return selectedService;
    }

    public List<ETT_FormServices> getListData() {
        return listData;
    }

    public void setSelectedRow(ETT_FormServices selectedService) {
        this.selectedService = selectedService;
    }

    public void setListData(List<ETT_FormServices> listData) {
        this.listData = listData;
    }

    public ETT_FormServices getETT_FormServices() {
        return ETT_FormServices;
    }

    public void setETT_FormServices(ETT_FormServices ETT_FormServices) {
        this.ETT_FormServices = ETT_FormServices;
    }

    public ETT_FormServices getETT_FormServices_old() {
        return ETT_FormServices_old;
    }

    public ETT_FormSystem getETT_System() {
        return ETT_System;
    }

    public List<ETT_FormSystem> getListUser2Group() {
        return listUser2Group;
    }

    public void setETT_FormServices_old(ETT_FormServices ETT_FormServices_old) {
        this.ETT_FormServices_old = ETT_FormServices_old;
    }

    public void setETT_System(ETT_FormSystem ETT_System) {
        this.ETT_System = ETT_System;
    }

    public void setListUser2Group(List<ETT_FormSystem> listUser2Group) {
        this.listUser2Group = listUser2Group;
    }

    public boolean isBlSelected() {
        return blSelected;
    }

    public void setBlSelected(boolean blSelected) {
        this.blSelected = blSelected;
    }

    public ETT_FormServices getSelectedService() {
        return selectedService;
    }

    public ETT_FormSystem getSelectedSys() {
        return selectedSys;
    }

    public void setSelectedService(ETT_FormServices selectedService) {
        this.selectedService = selectedService;
    }

    public void setSelectedSys(ETT_FormSystem selectedSys) {
        this.selectedSys = selectedSys;
    }
}
